package com.qf.android.sport.fragment.macth;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.qf.android.sport.R;
import com.qf.android.sport.adapter.matchadapter.MatchRecyclerViewAdapter;
import com.qf.android.sport.entity.match.AllMatch;
import com.qf.android.sport.entity.match.PreContest;
import com.qf.android.sport.retrofit.MatchHttpService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 * 全部比赛
 */
public class AllMatchFragment extends Fragment {


    private RecyclerView mRecyclerView;
    private FrameLayout mStickyHeader;//吸顶布局
    private TextView mSHDate;
    private TextView mSHWeek;
    private TextView mSHTranslate;
    private String match;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all_match, container, false);
        initViews(view);//初始化控件

        Bundle bundle = getArguments();
        match = bundle.getString("match");

        return view;
    }

    //控件初始化
    private void initViews(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_match_id);
        //吸顶布局
        mStickyHeader = (FrameLayout) view.findViewById(R.id.sticky_header_view);
        mSHDate = (TextView) mStickyHeader.findViewById(R.id.tv_match_header_date);
        mSHWeek = (TextView) mStickyHeader.findViewById(R.id.tv_match_header_week);
        mSHTranslate = (TextView) mStickyHeader.findViewById(R.id.tv_match_header_translate);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //有关RecyclerView的操作
        aboutRecyclerViewOperation();

    }

    private void aboutRecyclerViewOperation() {

        MatchHttpService allMatchHttpService = new Retrofit.Builder()
                .baseUrl("http://api.lifxapi.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(MatchHttpService.class);
        Call<AllMatch> MatchService = null;
        switch (match) {
            case "HotMatch":
                MatchService = allMatchHttpService.getHotMatch();
                break;
            case "AllMatch":
                MatchService = allMatchHttpService.getAllMatch();
                break;
            case "NBAMatch":
                MatchService = allMatchHttpService.getNBAMatch();
                break;
            case "CFMatch":
                MatchService = allMatchHttpService.getCFMatch();
                break;
            case "IFMatch":
                MatchService = allMatchHttpService.getIFMatch();
                break;
            case "CBMatch":
                MatchService = allMatchHttpService.getCBMatch();
                break;
        }

        MatchService.enqueue(new Callback<AllMatch>() {
            @Override
            public void onResponse(Call<AllMatch> call, Response<AllMatch> response) {
                AllMatch allMatch = response.body();
                //准备数据源
                List<PreContest> contest_list = allMatch.getData().getContest_list();
                //准备适配器
                MatchRecyclerViewAdapter adapter = new MatchRecyclerViewAdapter(getContext(), contest_list);
                //绑定布局管理器和适配器
                mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                mRecyclerView.setAdapter(adapter);
                //添加滚动监听
                mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        View stickyView = recyclerView.findChildViewUnder(mStickyHeader.getMeasuredWidth() / 2, 5);
                        TextView mTvDate = (TextView) stickyView.findViewById(R.id.tv_match_header_date);
                        TextView mTvWeek = (TextView) stickyView.findViewById(R.id.tv_match_header_week);
                        TextView mTvTranslate = (TextView) stickyView.findViewById(R.id.tv_match_header_translate);
                        if (stickyView != null) {
                            mSHDate.setText(mTvDate.getText().toString());
                            mSHWeek.setText(mTvWeek.getText().toString());
                            mSHTranslate.setText(mTvTranslate.getText().toString());
                        }
                        View transView = recyclerView.findChildViewUnder(mStickyHeader.getMeasuredWidth() / 2, mStickyHeader.getMeasuredHeight() + 1);
                        if (transView != null && transView.getTag() != null) {
                            int transViewStatus = (int) transView.getTag();
                            int dealtY = transView.getTop() - mStickyHeader.getMeasuredHeight();
                            if (transViewStatus == MatchRecyclerViewAdapter.HAS_STICKY_VIEW) {
                                if (transView.getTop() > 0) {
                                    mStickyHeader.setTranslationY(dealtY);
                                } else {
                                    mStickyHeader.setTranslationY(0);
                                }
                            } else if (transViewStatus == MatchRecyclerViewAdapter.NONE_STICKY_VIEW) {
                                mStickyHeader.setTranslationY(0);
                            }
                        }

                    }
                });
            }

            @Override
            public void onFailure(Call<AllMatch> call, Throwable t) {
                t.printStackTrace();
            }
        });

    }

}


